package com.minyifang.wcspf.work.ui.home.fragments;


import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.minyifang.wcspf.R;
import com.minyifang.wcspf.common.base.BaseMvpFragment;
import com.minyifang.wcspf.common.utils.ArrayUtil;
import com.minyifang.wcspf.work.ui.home.activities.SearchActivity;
import com.minyifang.wcspf.work.ui.home.adapter.ChooseClassAdapter;
import com.minyifang.wcspf.work.ui.home.fragments.home2sub.GoodsListFragment;
import com.minyifang.wcspf.work.ui.home.model.ClassBean;
import com.minyifang.wcspf.work.ui.home.presenter.HomePage2Presenter;
import com.minyifang.wcspf.work.ui.home.view.HomePage2View;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 商品分类
 */
public class HomePage2Fragment extends BaseMvpFragment<HomePage2View, HomePage2Presenter> implements HomePage2View, ChooseClassAdapter.OnItemClickListener {

    @BindView(R.id.rv_subclass)
    RecyclerView mRecyclerView;
    @BindView(R.id.ll_net_error)
    LinearLayout mLlError;
    @BindView(R.id.tl_class)
    TabLayout mTabLayout;

    private List<String> mClassTitleList = new ArrayList<>();
    private List<ClassBean> mClassList = new ArrayList<>();
    private ChooseClassAdapter mChooseClassAdapter;
    private GoodsListFragment mGoodsListFragment;
    private ClassBean mCurrentClass;

    public HomePage2Fragment() {
    }

    @Override
    protected int setLayoutResource() {
        return R.layout.fragment_home_page2;
    }

    @Override
    protected HomePage2Presenter createPresenter() {
        return new HomePage2Presenter();
    }

    @Override
    protected void initView() {
        mGoodsListFragment = (GoodsListFragment) getChildFragmentManager().findFragmentById(R.id.fragment_list);
        mChooseClassAdapter = new ChooseClassAdapter(mClassTitleList, getContext());
        mRecyclerView.setAdapter(mChooseClassAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mChooseClassAdapter.setOnItemClickListener(this);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (mCurrentClass == null)
                    return;
                updateOnlySubClass(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    @Override
    protected void initData() {
        getPresenter().getClassList();
    }

    @Override
    protected void lazyLoadData() {
    }

    @Override
    public void onGetClassListSucceed(List<ClassBean> classBeanList) {
        mClassList.clear();
        mClassList.addAll(classBeanList);
        updateClassAndGoodsListData(0, 0);
    }

    @Override
    public void onItemClick(int position) {
        updateClassAndGoodsListData(position, 0);
    }

    public void updateClassAndGoodsListData(int fatherPosition, int subPosition) {
        mClassTitleList.clear();
        mClassTitleList.addAll(ArrayUtil.listConvertToStringList(mClassList, classBean -> classBean.name));
        mChooseClassAdapter.notifyDataSetChanged();
        mChooseClassAdapter.setCurChosePosition(fatherPosition);
        mCurrentClass = mClassList.get(fatherPosition);
        mTabLayout.removeAllTabs();
        for (String s : ArrayUtil.listConvertToStringList(mClassList.get(fatherPosition).subClassBeans, subClassBean -> subClassBean.name)) {
            mTabLayout.addTab(mTabLayout.newTab().setText(s));
        }
        TabLayout.Tab tab = mTabLayout.getTabAt(subPosition);
        if (tab != null) {
            tab.select();
        }
    }

    public void updateOnlySubClass(int position) {
        mGoodsListFragment.updateFilterAndGoodsData(mCurrentClass.subClassBeans.get(position));
    }

    @OnClick(R.id.tv_get_data)
    void tv_get_data() {
        mLlError.setVisibility(View.GONE);
        getPresenter().getClassList();
    }

    @OnClick(R.id.tv_search)
    void tv_search() {
        startActivity(new Intent(getContext(), SearchActivity.class));
    }

    public void checkClassByClassId(String classId) {
        if (mClassList == null) return;
        for (int i = 0; i < mClassList.size(); i++)
            if (classId.equals(mClassList.get(i).id)) {
                mChooseClassAdapter.setCurChosePosition(i);
                break;
            }
    }

    public void checkClassBySubClassId(String classId, String subClassId) {
        if (mClassList == null) return;
        out:
        for (int i = 0; i < mClassList.size(); i++) {
            ClassBean classBean = mClassList.get(i);
            if (classId.equals(classBean.id)) {
                List<ClassBean.SubClassBean> subClassBeanList = classBean.subClassBeans;
                for (int j = 0; j < subClassBeanList.size(); j++) {
                    if (subClassId.equals(subClassBeanList.get(j).id)) {
                        updateClassAndGoodsListData(i, j);
                        break out;
                    }
                }
                break;
            }
        }
    }

    public void initiativeRefreshGoodsList(){
        mGoodsListFragment.refreshGoodsList();
    }

    @Override
    public void showLoadingView() {

    }

    @Override
    public void hideLoadingView() {

    }

    @Override
    public void onNetworkError() {
        mLlError.setVisibility(View.VISIBLE);
    }
}
